<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['status']){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['mid']){
        $params['mid']=intval($_RQ['mid']);
        $return['params']['mid']=$_RQ['mid'];
    }
    if($_RQ['id']){
        $params['id']=intval($_RQ['id']);
        $return['params']['id']=$_RQ['id'];
    }
    if(!empty($_RQ['expresssn'])){
        $params['expresssn like']='%'.trim($_RQ['expresssn']).'%';
        $return['params']['expresssn']=$_RQ['expresssn'];
    }
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc'
	);
	$list=$this->get_list('jilog',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['address']=json_decode($item['address'],true);
			$pcd=$item['address']['pcd'];
			$item['pcd']=$pcd['province'].' '.$pcd['city'].' '.$pcd['district'].' '.$pcd['street'];
			$item['toaddress']=json_decode($item['toaddress'],true);
			$pcd=$item['toaddress']['pcd'];
			$item['topcd']=$pcd['province'].' '.$pcd['city'].' '.$pcd['district'].' '.$pcd['street'];
            $item['fromuser']=$this->pdo_getcolumn('member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']),'fromuser');
            $item['fromuser']=$item['fromuser']?:'0';
            $item['fromuser2']=$this->pdo_getcolumn('commission',array('uniacid'=>$_SESSION['uniacid'],'type'=>2,'orderid'=>$item['id']),'mid');
            $item['fromuser2']=$item['fromuser2']?:'0';
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'记录管理'),
		),
		'list'=>array('data'=>'list','tip'=>'接单失败原因：1、云洋那边余额不能低于100.00元；2、地址必须完整！','edit'=>false,'list'=>array(
            array('title'=>'会员ID_member ID','type'=>'img_string',
                'data'=>array(
                    array('title'=>'直推ID：','name'=>'fromuser'),
                    array('title'=>'团队奖ID：','name'=>'fromuser2')
                )
            ),
		    array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'pcd','title'=>'寄件地址','type'=>'string'),
			array('name'=>'topcd','title'=>'收件地址','type'=>'string'),
			array('name'=>'money','title'=>'费用','type'=>'string'),
			array('name'=>'baoprice','title'=>'保价','type'=>'string'),
			array('name'=>'koufee','title'=>'运费成本','type'=>'string'),
			array('name'=>'bufee','title'=>'补价','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'jilog_status'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'href','op'=>'look','title'=>'查看'),
			array('type'=>'post','op'=>'cancel','title'=>'取消订单','show'=>array('status'=>0)),
			array('type'=>'post','op'=>'refund','title'=>'取消订单','show'=>array('status'=>array(1,2))),
			array('type'=>'post','op'=>'success','title'=>'完成订单','show'=>array('status'=>array(2))),
		)),
		'search'=>array(
			array('type'=>'radio','width'=>15,'name'=>'status','title'=>'状态','options'=>'jilog_status'),
            array('name'=>'id','width'=>15,'title'=>'订单ID','type'=>'string'),
            array('name'=>'expresssn','width'=>15,'title'=>'快递单号','type'=>'string'),
			array('type'=>'choose','width'=>18,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
		),
	);
	$return['htmls']=$htmls;
	$return['jilog_status']=$this->jilog_status;
	exi($return);
}
if($_RQ['op']=='look'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_kuaidisys_jilog',array('id'=>$id));
		$item['lat_lng']=array('lat'=>$item['lat'],'lng'=>$item['lng']);
	}else{
		$item=array();
	}
    $item['member']=cfc('core')->member_show($item['mid']);
	$item['address']=json_decode($item['address'],true);
	$pcd=$item['address']['pcd'];
	$item['pcd']=$pcd['province'].' '.$pcd['city'].' '.$pcd['district'].' '.$pcd['street'].' '.$item['address']['address'];
	$item['toaddress']=json_decode($item['toaddress'],true);
	$pcd=$item['toaddress']['pcd'];
	$item['topcd']=$pcd['province'].' '.$pcd['city'].' '.$pcd['district'].' '.$pcd['street'].' '.$item['toaddress']['address'];
	$item['upass']='';
    $return['item']=$item;	
	$htmls=array( 
		'type'=>'look',
		'post'=>'post',
		'desc'=>'记录设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回记录列表'),
		),
		'look'=>array(
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
            array('name'=>'address.name','title'=>'寄件人','type'=>'string'),
            array('name'=>'address.mobile','title'=>'寄件人电话','type'=>'string'),
			array('name'=>'pcd','title'=>'寄件地址','type'=>'string'),
			array('name'=>'topcd','title'=>'收件地址','type'=>'string'),
			array('name'=>'money','title'=>'费用','type'=>'string'),
			array('name'=>'baoprice','title'=>'保价','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'jilog_status'),
			array('name'=>'code_msg','title'=>'失败原因','type'=>'string','show'=>array('status'=>5)),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		)
	);
	$return['jilog_status']=$this->jilog_status;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='cancel'){
	$id=intval($_RQ['id']);
	$this->model('model')->jilog_cancel($id);	
	exi('取消成功！');	
}
if($_RQ['op']=='refund'){
	$id=intval($_RQ['id']);
	$order=$this->pdo_get('jilog',array('id'=>$id));
	$params=array(
			'type'=>0,
			'orderid'=>$id,
			'price'=>$order['money'],
			'content'=>'管理员提交',
	);
	$this->model('refund')->refund_create($params);
	exi('取消成功！');	
}
if($_RQ['op']=='success'){
	$id=intval($_RQ['id']);
	$log=pdo_get('jy_kuaidisys_jilog',array('id'=>$id));
	pdo_update('jy_kuaidisys_jilog',array('status'=>3),array('id'=>$id));		
	$this->model('commission')->order_complete($log);
	exi('手动完成订单成功！');	
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	$post=get_postdata();
	pdo_update('jy_kuaidisys_jilog',array('stop'=>intval($post['stop'])),array('id'=>$id));	
	exi('设置成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		$this->delete('jilog',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$this->delete('jilog',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>